package com.example.dell.fastshopping.views;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;


public abstract class BaseListFragmnet extends Fragment {
    protected int pageNum = 1;
    protected Context mContex;
    protected int pageSize = 10;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContex = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }


    public void initData() {

        TwinklingRefreshLayout refreshLayout = getRefreshLayout();
        ProgressLayout progressLayout = new ProgressLayout(mContex);
        refreshLayout.setHeaderView(progressLayout);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                pageNum++;
                getNetData(pageNum, pageSize);
            }

            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNum = 1;
                getNetData(pageNum, pageSize);
            }
        });
    }

    protected abstract void getNetData(int page, int pageSize);

    protected abstract TwinklingRefreshLayout getRefreshLayout();

    protected void loadFinsh() {
        if (pageNum == 1) {
            getRefreshLayout().finishRefreshing();
        } else {
            getRefreshLayout().finishLoadmore();
        }
    }
}
